package com.zhoug.common3.view.dialog;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.widget.ListView;

import com.zhoug.common3.R;

import java.util.List;

import androidx.annotation.NonNull;

/**
 * 列表对话框的基类
 *
 * @Author 35574
 * @Date 2020/7/21
 * @Description
 */
public abstract class ZAbsListDialog<T> extends ZBaseDialog {
    private static final String TAG = ">>>ZAbsListDialog";
    private static final boolean DEBUG = false;
    /***数据集合*/
    private List<T> mData;
    /***Item文字对齐方式*/
    private int textGravity;
    /***Item文字大小*/
    private float textSize;
    /***Item文字颜色*/
    private int textColor;
    /***item分割线颜色*/
    private int dividerColor;
    /***item分割线高度*/
    private int dividerHeight;
    /***是否显示分割线*/
    private boolean showDivider;
    private ListView mListView;

    public ZAbsListDialog(@NonNull Context context) {
        this(context, 0);
    }

    public ZAbsListDialog(@NonNull Context context, int themeResId) {
        this(context, themeResId, 0, 0);
    }

    protected ZAbsListDialog(@NonNull Context context, int themeResId, int defStyleAttr, int defStyleRes) {
        super(context, themeResId, defStyleAttr, defStyleRes);
    }

    @Override
    protected void initialize(@NonNull Context context, int defStyleAttr, int defStyleRes) {
        textGravity = Gravity.CENTER;
        dividerHeight = 1;
        showDivider = true;
        super.initialize(context, defStyleAttr, defStyleRes);
        DialogAttributeParser.parseZMUIAbsListDialogAttrs(this, context, defStyleAttr, defStyleRes);
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.z_list_dialog;
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        mListView = findViewById(R.id.listView);
        //是否显示分割线
        if (showDivider) {
            ColorDrawable colorDrawable = new ColorDrawable();
            colorDrawable.setColor(dividerColor);
            mListView.setDivider(colorDrawable);
            mListView.setDividerHeight(dividerHeight);
        }
        mListView.setSelected(true);
        initListView(mListView);
    }

    /**
     * 滚动到指定位置
     *
     * @param position
     */
    protected void scrollToSelected(int position) {
        if (mListView != null && getData() != null && getData().size() > 0) {
            Log.d(TAG, "initListView:设置选中位置:" + position);
            if (position >= 0 && position < getData().size()) {
                mListView.setSelection(position);
            } else {
                mListView.setSelection(0);
            }
        }
    }

    protected abstract void initListView(ListView listView);

    public abstract String getText(T itemData);


    public List<T> getData() {
        return mData;
    }

    /**
     * 获取指定item项的数据
     *
     * @param position 位置
     * @return T
     */
    public T getItemData(int position) {
        if (mData != null) {
            return mData.get(position);
        }
        return null;
    }

    public void setData(List<T> data) {
        this.mData = data;
    }

    protected int getTextGravity() {
        return textGravity;
    }

    public void setTextGravity(int textGravity) {
        this.textGravity = textGravity;
    }

    protected float getTextSize() {
        return textSize;
    }

    public void setTextSize(float textSize) {
        this.textSize = textSize;
    }

    protected int getTextColor() {
        return textColor;
    }

    public void setTextColor(int textColor) {
        this.textColor = textColor;
    }

    protected int getDividerColor() {
        return dividerColor;
    }

    public void setDividerColor(int dividerColor) {
        this.dividerColor = dividerColor;
    }

    public void setDividerHeight(int dividerHeight) {
        this.dividerHeight = dividerHeight;
    }

    public void setShowDivider(boolean showDivider) {
        this.showDivider = showDivider;
    }

    protected ListView getListView() {
        return mListView;
    }
}
